package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Logic.ProcessHandler;
import pl.edu.pw.MidEmu.UI.Controllers.ApplicationsOutputsController;
import pl.edu.pw.MidEmu.UI.Views.ApplicationsOutputsView;

/**
 * Frame with GUI for application outputs
 * @author Mateusz Starodab
 *
 */
public class ApplicationOutputsFrame extends BaseFrame<JFrame, ApplicationsOutputsController> implements ApplicationsOutputsView
{
	private JButton btnClose;

	private MidasComponent parent;
	private JTabbedPane pnlCenter;
	private JPanel pnlContent;

	private JPanel pnlSouth;

	public ApplicationOutputsFrame(MidasComponent parent)
	{
		ApplicationsOutputsController controller = new ApplicationsOutputsController();
		controller.setView(this);
		setController(controller);
		JFrame frame = new JFrame();
		setComponent(frame);

		this.parent = parent;

		getComponent().setTitle(Consts.MainFrame_Title + " - " + this.parent.getName() + " - " + Consts.ApplicationsOutputs_Title);
		getComponent().setSize(800, 480);
		getComponent().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		pnlContent = new JPanel();
		pnlContent.setLayout(new BorderLayout());

		pnlCenter = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);

		List<ProcessHandler> temp = new ArrayList<ProcessHandler>();

		if (parent.getController().getMidasHandler() != null)
			temp.add(parent.getController().getMidasHandler());

		temp.addAll(parent.getController().getApplications());

		int i = 0;
		for (ProcessHandler app : temp)
		{

			JPanel panel = new JPanel();
			panel.setName(app.getAppName() + " - " + (app.isActive() ? "Active" : "Inactive"));
			panel.setLayout(new BorderLayout());

			app.subscribeForActivityChange(panel, i);

			JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

			JPanel topPanel = new JPanel();
			topPanel.setLayout(new BorderLayout());

			JPanel topLabelPanel = new JPanel();
			topLabelPanel.setLayout(new FlowLayout());

			JLabel topLabel = new JLabel(app.getAppName() + " - Output");
			topPanel.add(topLabel, BorderLayout.NORTH);

			JTextPane topText = new JTextPane();
			app.subscribeForOuputData(topText);

			JScrollPane topTextPanel = new JScrollPane(topText);

			topPanel.add(topTextPanel, BorderLayout.CENTER);

			pane.add(topPanel);

			JPanel bottomPanel = new JPanel();
			bottomPanel.setLayout(new BorderLayout());

			JPanel bottomLabelPanel = new JPanel();
			bottomLabelPanel.setLayout(new FlowLayout());

			JLabel botomLabel = new JLabel(app.getAppName() + " - Error");
			bottomPanel.add(botomLabel, BorderLayout.NORTH);

			JTextPane botomText = new JTextPane();
			app.subscribeForErrorData(botomText);

			JScrollPane bottomTextPanel = new JScrollPane(botomText);

			bottomPanel.add(bottomTextPanel, BorderLayout.CENTER);

			pane.add(bottomPanel);

			panel.add(pane, BorderLayout.CENTER);

			pnlCenter.add(panel);

			pane.setOneTouchExpandable(true);
			pane.setDividerLocation(0.5);
			pane.setResizeWeight(0.5);
			i++;
		}
		pnlContent.add(pnlCenter, BorderLayout.CENTER);

		pnlSouth = new JPanel();
		pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));

		btnClose = new JButton(Consts.ApplicationsOutputs_CloseButton);
		btnClose.setName(Consts.ApplicationsOutputs_CloseButton);
		btnClose.addActionListener(getController());
		pnlSouth.add(btnClose);

		pnlContent.add(pnlSouth, BorderLayout.SOUTH);

		getComponent().setContentPane(pnlContent);
	}

	@Override
	public void close()
	{
		getComponent().setVisible(false);
		getComponent().dispose();
	}

	public void setVisibleTab(String className)
	{
		for (int i = 0; i < pnlCenter.getTabCount(); i++)
			if (pnlCenter.getTitleAt(i).contains(className))
				pnlCenter.setSelectedIndex(i);
	}
}
